<?php
class CouponsController extends NodeController {
	
	/**
	 *审计列表
	 */
	public function audit() {
		//统计当前用户总账
		$rs = self::getOrderService ()->audit ();
		//获取审计账目 最近30条
		$audit = self::getOrderService ()->getAudit ( 30 );
		$this->view ()->assign ( "coupons", $rs );
		$this->view ()->assign ( 'audit', $audit );
		$this->view ()->display ( 'file:order/audit.tpl' );
	}
	/**
	 * 提交审计
	 * Enter description here ...
	 */
	public function setAudit() {
		
		$rs = self::getOrderService ()->setAudit ( $_POST );
		
		$this->sendNotice ( $rs );
	
	}
	
	/**
	 * 导出明细
	 * */
	public function export() {
		$this->setCsvHeader ();
		/*按照时间到处*/
		if (isset ( $_POST ['uid'] ) && $_POST ['uid'] > 0) {
			$where = array ('a.status' => 8, 'a.uid' => $_POST ['uid'] );
			$userinfo = MemberModel::instance ()->getOneMember ( array ('uid' => $_POST ['uid'] ) );
		} else {
			$where = array ('a.status' => 8 );
			$userinfo ['real_name'] = "全部";
		}
		$starttime = strtotime ( str_replace ( ".", "-", $_POST ['starttime'] ) );
		$endtime = strtotime ( str_replace ( ".", "-", $_POST ['endtime'] . " 23:59:59" ) );
		$shijianduan = $_POST ['starttime'] . "-" . $_POST ['endtime'] . " 23:59:59";
		//统计期初
		$beforeIncome = MemberModel::instance ()->getIncomeByBeforeTime ( $starttime, $where );
		$beforeSpend = MemberModel::instance ()->getSpendByBeforeTime ( $starttime, $where );
		$income = MemberModel::instance ()->getIncomeByTime ( $starttime, $endtime, $where );
		$spend = MemberModel::instance ()->getSpendByTime ( $starttime, $endtime, $where );
		$detail = MemberModel::instance ()->getCouponsHistoryAll ( $starttime, $endtime, $where );
		$downloadtime = date ( "Y-m-d H:i:s", time () );
		$baseinfo = "姓名,%s\r\n时间段,%s\r\n期初,%s\r\n订单数,%s\r\n积分收入,%s\r\n积分支出,%s\r\n积分余额,%s\r\n下载时间,%s";
		$couponsBalance = $beforeIncome ['income'] + $beforeSpend ['spend'] + $income ['income'] + $spend ['spend'];
		echo sprintf ( $baseinfo, $userinfo ['real_name'], $shijianduan, $beforeIncome ['income'] + $beforeSpend ['spend'], count ( $detail ), $income ['income'], $spend ['spend'], $couponsBalance, $downloadtime );
		echo "\r\n";
		echo $title = "流水号,充值时间,渠道,名称,收款人,军区,城市,收入,支出,收支,状态";
		echo "\r\n";
		$foramt = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s";
		$payment = array_flip ( $this->payment );
		$chargeType = array_flip ( $this->chargeType );
		foreach ( $detail as $v ) {
			$v ['status'] = $v ['status'] == 8 ? "成功" : "失败";
			if ($v ['payment'] == 1) {
				echo sprintf ( $foramt, $v ['id'], date ( "Y-m-d H:i:s", $v ['date'] ), $chargeType [$v ['chargetypes']], $v ['remark'], $v ['real_name'], $v ['country'], $v ['area'], "", $v ['coupons'], $payment [$v ['payment']], $v ['status'] );
			} else {
				echo sprintf ( $foramt, $v ['id'], date ( "Y-m-d H:i:s", $v ['date'] ), $chargeType [$v ['chargetypes']], $v ['remark'], $v ['real_name'], $v ['country'], $v ['area'], $v ['coupons'], "", $payment [$v ['payment']], $v ['status'] );
			}
			echo "\r\n";
		}
	}
	
	//充值明细
	public function charge() {
		// 默认设置为15条
		if ($_GET ['num'] > 0) {
			$num = trim ( $_GET ['num'] );
		} else {
			$newNum = require 'config/base.php';
			$num = 40;
		}
		$totalNum = OrderModel::instance ()->countOrderHisotry ();
		$page = $this->page ( $totalNum, $num );
		$this->view ()->assign ( 'num', $page );
		$this->view ()->assign ( 'totalnum', $totalNum );
		if (isset ( $_GET ['uid'] ) && $_GET ['uid'] != "") {
			$detail = CouponsModel::instance ()->getCouponsHistory ( array ('uid' => trim ( $_GET ['uid'] ) ) );
			$detail = CouponsModel::instance ()->getCouponsHistoryPage ( $page ['start'], $page ['num'], array ('a.uid' => $_GET ['uid'] ) );
			$user = MemberModel::instance ()->getOneMember ( array ('uid' => trim ( $_GET ['uid'] ) ) );
			$this->view ()->assign ( "userinfo", $user );
			$this->view ()->assign ( "uid", $_GET ['uid'] );
			$this->view ()->assign ( "mx", $this->matchChargeTypes ( $detail ) );
			$this->view ()->display ( "file:member/mx.tpl" );
		} else {
			
			$lastMoth = strtotime ( "-1 month" );
			$now = time ();
			$this->view ()->assign ( "time", array ('lastmonth' => date ( "Y.m.d", $lastMoth ), 'now' => date ( "Y.m.d", $now ) ) );
			$detail = CouponsModel::instance ()->getCouponsHistoryPage ( $page ['start'], $page ['num'] );
			$this->view ()->assign ( "mx", $this->matchChargeTypes ( $detail ) );
			$this->view ()->display ( "file:member/coupons.tpl" );
		}
	}
	
	/**
	 * 导入代码
	 * Enter description here ...
	 */
	public function import() {
		$action = $_GET ['action'];
		
		if ($action != 'import') {
			return;
		}
		$filename = $_FILES ['file'] ['tmp_name'];
		if (empty ( $filename )) {
			echo '请选择要导入的CSV文件！';
			exit ();
		}
		$remark = trim ( $_POST ['remark'] );
		$handle = fopen ( $filename, 'r' );
		$result = $this->input_csv ( $handle ); //解析csv 
		$len_result = count ( $result );
		if (! $len_result) {
			echo '没有任何数据！';
			exit ();
		}
		$notMatchFile = "";
		$line = "";
		for($i = 0; $i < $len_result; $i ++) { //循环获取各字段值 
			$name = iconv ( 'gb2312', 'utf-8', $result [$i] [0] ); //中文转码 
			$coupons = $result [$i] [1];
			$user = MemberModel::instance ()->getOneMember ( array ('real_name' => trim ( $name ), 'verify' => 1 ) );
			//如果没有找到
			$format = "%s,%s,%s";
			if (empty ( $user )) {
				$line = sprintf ( $format, $result [$i] [0], $coupons, "not found" );
				$notMatchFile = $notMatchFile . $line . "\r\n";
				continue;
			}
			$row = MemberModel::instance ()->saveCoupons ( $user ['uid'], $coupons, $remark, $this->payment ["收入"], $this->chargeType ["充值"], null, $this->status ['成功'] );
			//存储没有导入的数据
			if ($row < 1) {
				$line = sprintf ( $format, $result [$i] [0], $coupons, "not write" );
				$notMatchFile = $notMatchFile . $line . "\r\n";
				continue;
			}
		}
		$data_values = substr ( $data_values, 0, - 1 ); //去掉最后一个逗号 
		fclose ( $handle ); //关闭指针 
		$this->createCSV ( $notMatchFile );
		exit ();
	
	}
	
	public function addCoupons() {
		
		if (isset ( $_POST )) {
			$uid = $_POST ['uid'];
			$coupons = trim ( $_POST ['coupons'] );
			$remark = trim ( $_POST ['remark'] );
			$row = MemberModel::instance ()->saveCoupons ( $uid, $coupons, $remark, $this->payment ["收入"], $this->chargeType ["充值"], null, $this->status ['成功'] );
			if ($row > 0) {
				$this->sendNotice ( "充值成功" );
			} else {
				$this->sendNotice ( "充值失败" );
			}
		}
	}
	/*
     * 导入积分
     * */
	public function csv() {
		$this->view ()->display ( "file:order/csv.tpl" );
	}
	/**
	 * 用户前端查询
	 *
	 */
	public function coupons() {
		if (! isset ( $_SESSION [uid] )) {
			return;
		}
		// 默认设置为15条
		if ($_GET ['num'] > 0) {
			$num = trim ( $_GET ['num'] );
		} else {
			$newNum = require 'config/base.php';
			$num = 100;
		}
		$totalNum = 200;
		$page = $this->page ( $totalNum, $num );
		$user = $this->getUserInfo ();
		$detail = CouponsModel::instance ()->getCouponsHistoryPage ( $page ['start'], $page ['num'], array ('a.uid' => $user ['uid'] ) );
		$this->view ()->assign ( "mx", $this->matchChargeTypes ( $detail ) );
		$this->view ()->assign ( 'userinfo', $user );
		$this->view ()->assign ( 'totalnum', $totalNum );
		$this->view ()->assign ( 'num', $page );
		$this->view ()->display ( "mysql:coupons.tpl" );
	}

}